import { GlobalStore } from "../store/global";
import { BaseToolProvider } from "./base";
import { BrowserToolProvider } from "./browser";
import { FileToolProvider } from "./file";
import { InteractionToolProvider } from "./interaction";
import { MysqlToolProvider } from "./mysql";
import { TerminalToolProvider } from "./terminal";
import * as vscode from 'vscode';
import { Tool, ToolCall } from "./types";

export class ToolProviderFactory {
	static async createToolProvider(tool: ToolCall, context: vscode.ExtensionContext): Promise<BaseToolProvider> {
		const globalStore = new GlobalStore(context);
		const database = await globalStore.getDatabase();
		switch (tool.name) {
			case 'execute_sql':
			case 'get_table_name':
			case 'get_table_desc':
			case 'get_table_schema':
			case 'get_chinese_initials':
			case 'get_table_lock':
				switch(database.type) {
					case 'mysql': {
							const mysql = new MysqlToolProvider();
							mysql.connectionString = database.connectionString;
							return mysql;
					}
					default:
						return new BaseToolProvider();
				}
			case 'write_to_file':
			case 'replace_in_file':
			case 'delete_file':
			case "read_file":
			case 'list_files':
			case 'search_files':
				return new FileToolProvider();
			case 'execute_command':
			case 'list_code_definition_names':
				return new TerminalToolProvider();
			case 'browser_action':
				return new BrowserToolProvider();
			case 'ask_followup_question':
			case 'attempt_completion':
				return new InteractionToolProvider();
			default:
				return new BaseToolProvider();
		}
	}

	static providers: BaseToolProvider[] = [];
	static registerProvider(provider: BaseToolProvider) {
		this.providers.push(provider);
	}
	static listTool() : Tool[] {
		return this.providers.flatMap(provider => provider.listTool());
	}


	static initialize() {
		this.registerProvider(new FileToolProvider());
		this.registerProvider(new BrowserToolProvider());
		this.registerProvider(new TerminalToolProvider());	
		this.registerProvider(new InteractionToolProvider());
		this.registerProvider(new MysqlToolProvider());
	}
}